package com.dy.exception;

import com.dy.response.Result;
import com.dy.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @program: xg-parent
 * @description: 统一全局处理异常类
 * @author: dy
 * @create: 2022-03-02 19:20
 **/
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 功能描述: <br>
     * 〈总异常〉
     * @Param: [e]
     * @Return: com.dy.result.Result
     * @Author: dy
     * @Date: 2022/3/2 19:35
     */
    @ExceptionHandler(Exception.class)
    public Result exception(Exception e){
        //e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.COMMON_FAIL.getCode())
                .message(ResultCode.COMMON_FAIL.getMessage());
    }

    /**
     * 功能描述: <br>
     * 〈算数异常〉
     * @Param: [e]
     * @Return: com.dy.result.Result
     * @Author: dy
     * @Date: 2022/3/2 19:35
     */
    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticException(ArithmeticException e){
//        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.ARITHMETIC_EXCEPTION.getCode())
                .message(ResultCode.ARITHMETIC_EXCEPTION.getMessage());
    }

    /**
     * 功能描述: <br>
     * 〈空指针异常〉
     * @Param: [e]
     * @Return: com.dy.result.Result
     * @Author: dy
     * @Date: 2022/3/2 19:35
     */
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e){
//        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.NULLPOINTER_EXCEPTION.getCode())
                .message(ResultCode.NULLPOINTER_EXCEPTION.getMessage());
    }

    /**
     * 功能描述: <br>
     * 〈数组越界异常〉
     * @Param: [e]
     * @Return: com.dy.result.Result
     * @Author: dy
     * @Date: 2022/3/2 19:35
     */
    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Result arrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e){
//        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.ARRAYINDEXOUTOFBOUNDS_EXCEPTION.getCode())
                .message(ResultCode.ARRAYINDEXOUTOFBOUNDS_EXCEPTION.getMessage());
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result indexOutOfBoundsException(IndexOutOfBoundsException e){
//        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.INDEXOUTOFBOUNDS_EXCEPTION.getCode())
                .message(ResultCode.INDEXOUTOFBOUNDS_EXCEPTION.getMessage());
    }

    @ExceptionHandler(ArrayStoreException.class)
    public Result arrayStoreException(ArrayStoreException e){
//        e.printStackTrace();
        log.error(e.getMessage());
        return Result.error().code(ResultCode.ARRAYSTORE_EXCEPTION.getCode())
                .message(ResultCode.ARRAYSTORE_EXCEPTION.getMessage());
    }

    /**
     * 功能描述: <br>
     * 〈〉
     * @Param: [e]
     * @Return: com.dy.result.Result
     * @Author: dy
     * @Date: 2022/3/2 19:36
     */
    @ExceptionHandler(BusinessException.class)
    public Result error(BusinessException e){
        //e.printStackTrace();
        log.error(e.getErrMsg());
        return Result.error().code(e.getCode())
                .message(e.getErrMsg());
    }
}
